﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using Visifire.Charts;
using Marking.Dashboard.Infrastructures;
using Marking.Dashboard.ViewModels.Sales;

namespace Marking.Dashboard.Views.Sales
{
    public partial class SalesLineChartView : UserControl
    {
        public SalesLineChartView()
        {
            InitializeComponent();
        }

        public event EventHandler<SalesInfoClickEventArgs> SalesInfoClick;

        protected void OnSalesInfoClick(SalesInfo salesInfo)
        {
            var handler = this.SalesInfoClick;

            if (handler != null)
            {
                handler(this, new SalesInfoClickEventArgs { SalesInfo = salesInfo });
            }
        }

        public static readonly DependencyProperty ReportUrlProperty = DependencyProperty.Register(
            "ReportUrl", typeof(string), typeof(SalesLineChartView), null);

        public string ReportUrl
        {
            get
            {
                return (string)this.GetValue(ReportUrlProperty);
            }
            set
            {
                this.SetValue(ReportUrlProperty, value);
            }
        }

        private void DataSeries_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            var data = (sender as DataPoint);

            var salesInfo = (this.DataContext as SalesLineChartViewModel)
                .FirstOrDefault(x => x.DisplayDateString == data.AxisXLabel);

            if (salesInfo == null)
            {
                return;
            }

            if (!string.IsNullOrWhiteSpace(this.ReportUrl))
            {
                ReportHelper.Show(this.ReportUrl, new
                {
                    StartDate = salesInfo.StartDate.ToString("yyyy-MM-dd"),
                    EndDate = salesInfo.EndDate.ToString("yyyy-MM-dd")
                });
            }

            this.OnSalesInfoClick(salesInfo);
        }
    }
}
